package org.ru.mse10.cvis.web.bean.action;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.ru.mse10.cvis.constants.UserRole;
import org.ru.mse10.cvis.entity.cv.User;
import org.ru.mse10.cvis.entity.misc.Identity;

/**
 * The Class BaseAction.
 * 
 * @param <E>
 *            the element type
 */
@SessionScoped
@ManagedBean(name = "baseAction")
public class BaseAction<E extends Identity> implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 8997153037439218558L;

	/** The permissions. */
	private Set<Integer> permissions = new HashSet<Integer>();

	/** The target entity. */
	private E targetEntity;

	/** The readonly. */
	private boolean readonly = true;

	/**
	 * Checks if is readonly.
	 * 
	 * @return the readonly
	 */
	public boolean isReadonly() {
		return readonly;
	}

	/**
	 * Sets the readonly.
	 * 
	 * @param readonly
	 *            the readonly to set
	 */
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	/**
	 * Gets the target entity.
	 * 
	 * @return the targetEntity
	 */
	public E getTargetEntity() {
		return targetEntity;
	}

	/**
	 * Sets the target entity.
	 * 
	 * @param targetEntity
	 *            the targetEntity to set
	 */
	public void setTargetEntity(E targetEntity) {
		this.targetEntity = targetEntity;
	}

	/**
	 * Gets the logged in.
	 * 
	 * @return the logged in
	 */
	public boolean getLoggedIn() {
		return getUser() != null;
	}

	/**
	 * Gets the user.
	 * 
	 * @return the user
	 */
	public User getUser() {
		return (User) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("user");
	}

	/**
	 * Sets the user.
	 * 
	 * @param user
	 *            the new user
	 */
	public void setUser(User user) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put("user", user);
		permissions.clear();

	}

	/**
	 * Gets the checks for admin role.
	 * 
	 * @return the checks for admin role
	 */
	public boolean getHasAdminRole() {
		return getUser() != null && UserRole.ADMIN.equals(getUser().getRole());
	}

	/**
	 * Gets the checks for user role.
	 * 
	 * @return the checks for user role
	 */
	public boolean getHasUserRole() {
		return getUser() != null && UserRole.USER.equals(getUser().getRole());
	}

	/**
	 * Gets the checks for manager role.
	 * 
	 * @return the checks for manager role
	 */
	public boolean getHasManagerRole() {
		return getUser() != null
				&& UserRole.MANAGER.equals(getUser().getRole());
	}

	/**
	 * Gets the checks for system role.
	 * 
	 * @return the checks for system role
	 */
	public boolean getHasSystemRole() {
		return getUser() != null && UserRole.SYSTEM.equals(getUser().getRole());
	}

	/**
	 * Gets the checks for inial role.
	 * 
	 * @return the checks for inial role
	 */
	public boolean getHasInialRole() {
		return getUser() != null
				&& UserRole.INITIAL.equals(getUser().getRole());
	}

	/**
	 * Gets the checks for permission.
	 * 
	 * @param permissionCode
	 *            the permission code
	 * @return the checks for permission
	 */
	public boolean getHasPermission(int permissionCode) {
		return permissions.contains(permissionCode);
	}
}
